Ext.define('Ext.ux.desktop.Desktop', {
	id: 'default-desktop',
	apps: {},
	appsTplConf: [],
	iconsRoot: '/desktop/resources/icons',
	
	constructor: function(confObj){
		this.init(confObj);
	},
	
	init: function(confObj){
		Ext.apply(this,confObj);
		this.viewSize = Ext.getBody().getViewSize();
		console.log(this.viewSize);
		this.setApplications();
		this.renderTpl();
		this.bindCalls();
	},
	
	setApplications: function(){
		for(var i in Ext.ux.desktop.applications){
			if(i != 'AbstractApplication'){
				this.apps[i] = Ext.create('Ext.ux.desktop.applications.'+i);
				this.appsTplConf.push({
					appName: i,
					shortcutID: 	this.apps[i].id+'-shortcut',
					shortcutTitle: 	this.apps[i].title
				});
			}
		}
	},
	
	renderTpl: function(){
		var _this = this;
		var xt = new Ext.XTemplate(
			'<div class="x-desktop-icons-area">',
				'<tpl for=".">',
			 		'<div class="x-desktop-icon-holder" id="{shortcutID}">',
			 			'<img src="'+this.iconsRoot+'/ico_syssettings.png" alt="" width="44" height="44" />',
			 			'<span class="x-desktop-icon-glass-effect"></span>',
			 			/*'<span class="x-desktop-icon-title">{[this.ellips(values.shortcutTitle)]}</span>',*/
			 		'</div>',
			 	'</tpl>',
			 	'<div style="clear:both;"></div>',
			'</div>',
			/*'<div class="x-desktop-task-row">',
				'<div class="x-desktop-task-row-left-arrow" style="float:left;">&lt;-</div>',
				'<div class="x-desktop-task-row-conteiner" style="float:left;"></div>',
				'<div class="x-desktop-task-row-right-arrow" style="float:right;">-&gt;</div>',
				'<div style="clear:both;"></div>',
		 	'</div>',*/
			{
				ellips: function(title){
					return _this.iconTitleEllipsis(title);
				}
			}
		);
		xt.overwrite(Ext.getBody(),this.appsTplConf,true);

		Ext.each(this.appsTplConf, function(item, index, self) {
			Ext.create('Ext.tip.ToolTip', {
				target: item['shortcutID'],
				html: item['shortcutTitle']
			});
		});
	},
	
	/*iconTitleFull:function(title){
		if(title.search(' ') < 0 || title.search(' ') > 8){
			title = title.substr(0,8)+' '+title.substr(8);
		}
		return title;
	},
	
	iconTitleEllipsis:function(title){
		var new_t = Ext.String.ellipsis(title,16);
		if(new_t.search(' ') < 0 || new_t.search(' ') > 8){
			new_t = new_t.substr(0,8)+' '+new_t.substr(8);
		}
		return new_t;
	},*/
	
	bindCalls: function(){
		for(var i in this.appsTplConf){
			//launch app
			Ext.get(this.appsTplConf[i].shortcutID).on("click",function(evt,el,o){
				this.callApplication(o.appName);
			},this,this.appsTplConf[i]);
			//title changing
			/*Ext.get(this.appsTplConf[i].shortcutID).on("mouseenter",function(evt,el,o){
				Ext.DomHelper.overwrite(Ext.get(o.shortcutID).down('span.x-desktop-icon-title',true),this.iconTitleFull(o.shortcutTitle));
			},this,this.appsTplConf[i]);
			Ext.get(this.appsTplConf[i].shortcutID).on("mouseleave",function(evt,el,o){
				Ext.DomHelper.overwrite(Ext.get(o.shortcutID).down('span.x-desktop-icon-title',true),this.iconTitleEllipsis(o.shortcutTitle));
			},this,this.appsTplConf[i]);*/
		}
	},
	
	callApplication: function(appName){
		if(this.apps[appName]){
			if(Ext.getCmp(this.apps[appName].id) == undefined){
				this.apps[appName].init();
			}else{
				this.focused(appName);
			}
		}
		else{
			Ext.Msg('Warning','Application not found');
			console.log('Desktop Notice > Application "'+appName+'" not found!');
		}
	},
	
	focused: function(appName){
		
	}
});
Ext.define('Ext.ux.desktop.applications.AbstractApplication', {
	id:'abstract-application',
	title: 'AbstractApplication',
	iCls: 'icon-abstracta-application',
	pageSize: 20,
	stores: {},
	grids: {},
	dialogs: {},
	scriptURL: null,
	
	init: function(confObj){
		Ext.apply(this,confObj);
		this.createWindow();
	},
	
	createWindow: function(){
		
	},
	
	setActiveButtons: function(docked,list){
		var count = docked.items.length;
		var btns = docked.items.items;
		for(i=0;i<count;i++){
			btns[i].setDisabled(list[btns[i].btnName]);
		}
	},
	
	confirmation_obj: {
		sm: null,
		grid: null,
		success: null,
		fail: null,
		url: null,
		id_field : 'id',
		onAccept: function(btn){
			if ("yes" == btn || !this.sm || !this.url){
				var id = null;
				var sc = this.sm.getCount();
				var selection = this.sm.getLastSelected();
				if (sc == 1){
					id = selection.data[this.id_field];
				} else {
					Ext.util.Helper.displayMessages(['Please, select one row.'], false);
					return;
				}
				if (!this.success) {
					var store = this.grid.getStore();
					this.success = {
						fn: store.load,
						scope: store
					};
				}
				Ext.util.Helper.request(this.url, {'id': id}, this.success, this.fail);
			}
		}
	}
});
Ext.define('Ext.ux.desktop.modules.EditDialog', {
	extend: 'Ext.window.Window',
	
	id: 'edit-dialog-window',
	title: 'Edit Dialog',
	iconCls: null,
	width:400,
	height:400,
	shim:false,
	animCollapse:false,
	constrainHeader:true,
	maximizable: false,
	closeAction: 'hide',
	modes: {
		list: {add:true,edit:true},
		current: null
	},
	modeTitles: {
		add: 'Add Item',
		edit: 'Edit Item'
	},
	modeUrls: null,
	layout: 'fit',
	items: {},

	constructor: function(conf){
		this.buttons = [
             		    {text: 'Save', handler: this.submitForm, scope: this, minWidth: 80},
            		    {text: 'Cancel', handler: this.onCancel, scope: this, minWidth: 80}
            		];
		this.addEvents('successSend','modeChange','dataSet');
		
		var form_conf = {
			listeners: {
				render: function(panel) {
					this.form = panel.getForm();
				},
				actioncomplete: function(form, action) {
					if (action.type == 'submit') {
						this.fireEvent('successSend', action.result);
						this.hide();
					} else if (action.type == 'load') {
						this.fireEvent('dataSet', this, action.result.data);
					}
				},
				scope:this
			}
		};

		Ext.apply(conf.items,form_conf);
		this.superclass.constructor.call(this,conf);
	},
	
	onModeChange: function(mode) {
		var titles = this.modeTitles;
		if (titles && titles[mode]) {
			this.setTitle(titles[mode]);
		}
		var urls = this.modeUrls;
		if (urls && urls[mode]) {
			this.current_url = urls[mode];
		}
	},

	setMode: function(mode) {
		if (!this.modes || !mode || !this.modes.list[mode]) {
			return false;
		}
		if(this.modes.current == mode) {
			return true;
		}
		this.onModeChange(mode);
		this.fireEvent('modeChange', this, mode);
		this.modes.current = mode;
		return true;
	},
	
	show: function(data){
		this.superclass.show.call(this);
		if (this.form) {
			this.form.reset();
		}
		//this.form = this.items.first().getForm();
		this.refreshBody(data);
	},
	
	refreshBody: function(data) {
		this.form.setValues(data);
		this.fireEvent('dataSet', this, data);
	},
	
	submitForm: function(){
		var o = {
				waitMsg: this.maskMessage ? this.maskMessage : 'Saving...',
				scope: this
		};
		if (this.current_url) {
			o.url = this.current_url;
		}

		this.form.submit(o);
	},
	onCancel: function(){
		this.hide();
	}
});